<!-- 查看图片 -->
<template>
	<div class="view">
		<div class="adds">
			{{ getAddress(rowData) }}
		</div>

		<div class="ImgBox" v-loading="!state.flag" element-loading-text="图片获取中...">
			<img v-if="state.imgUrl" :src="getFileUrl(state.imgUrl)" />
			<div v-if="state.flag && !state.imgUrl">加载失败</div>
		</div>

		<div class="btns">
			<div class="btn" @click="close(props.rowData.deviceCode)">
				<CircleClose />
			</div>

			<div class="btn" @click="wsGetImg(props.rowData.deviceCode)">
				<Refresh />
			</div>
		</div>
	</div>
</template>

<script lang="ts" setup name="ViewImage">
import { reactive, onMounted, ref, watch, onBeforeUnmount, nextTick, computed, defineProps } from 'vue';
import { Refresh, CircleClose } from '@element-plus/icons-vue';
import { useSmoke } from '/@/hook/index';
import { smokeStore } from '/@/stores/smoke';
const { getFileUrl, getAddress } = useSmoke();

// props
const props = defineProps({
	rowData: <any>{
		type: Object,
	},
});

const state = reactive({
	flag: false, //  是否超过指定获取时间
	imgUrl: '',
});

const emit = defineEmits(['closeImg']);

onMounted(() => {
	wsGetImg(props.rowData.deviceCode);
	setTimeout(() => {
		if (state.imgUrl == '') {
			state.flag = true;
		}
	}, 10000);
});

watch(
	() => smokeStore().deviceImgs,
	(newValue: any) => {
		let curImgUrl = smokeStore().deviceImgs[props.rowData.deviceCode];
		if (curImgUrl) {
			state.imgUrl = curImgUrl;
			state.flag = true;
		}
	},
	{
		deep: true,
	}
);

// ws 获取图片
function wsGetImg(deviceCode: string) {
	state.flag = false;
	setTimeout(() => {
		if (state.imgUrl == '') {
			state.flag = true;
		}
	}, 10000);
	let payload = {
		cmdType: 'getImg',
		deviceCode,
		data: null,
	};
	smokeStore().wsConn.send(JSON.stringify(payload));
}

function close(deviceCode: string) {
	emit('closeImg', deviceCode);
}
</script>
<style lang="scss" scoped>
.view {
	width: 100%;
	height: 100%;
	position: relative;
	display: flex;
	justify-content: center;
	align-items: center;
	overflow: hidden;
	background-color: #dedfe0;

	.adds {
		position: absolute;
		right: 0;
		top: 0;
		background: rgba($color: #000000, $alpha: 0.6);
		padding: 5px 10px;
		font-size: 14px;
		z-index: 100;
		color: rgba(255, 255, 255, 0.8);
	}

	.ImgBox {
		width: 100%;
		display: flex;
		justify-content: center;
		align-items: center;

		img {
			max-width: 100%;
			aspect-ratio: 16/9;
		}

		color: #f56c6c;
	}

	.btns {
		position: absolute;
		right: 0;
		z-index: 10;
		display: grid;
		gap: 20px;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		.btn {
			width: 40px;
			height: 40px;
			padding: 10px;
			background-color: rgba($color: #000000, $alpha: 0.5);
			color: #fff;

			&:nth-child(1) {
				color: #f56c6c;
			}

			&:nth-child(2) {
				color: #67c23a;
			}

			&:hover {
				background-color: rgba($color: #000000, $alpha: 0.8);
			}
		}
	}

	:deep(.el-loading-spinner){
		.circular {
			display: none;
		}
	}
}
</style>
